//
//  BaseTabbar.m
//  Mi_Cet4Listening
//
//  Created by lxl on 2019/3/29.
//  Copyright © 2019年 lxl. All rights reserved.
//

#import "BaseTabbar.h"

@implementation BaseTabbar{
    UIEdgeInsets _oldSafeAreaInsets;
}
//求救界面
- (void)helpShow{
    if ([self.delegate respondsToSelector:@selector(baseTabbarOneKeyHelpAction)]) {
        [self.delegate baseTabbarOneKeyHelpAction];
    }
}




- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _oldSafeAreaInsets = UIEdgeInsetsZero;
        self.translucent = NO;
        self.plusItem = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.plusItem addTarget:self action:@selector(helpShow) forControlEvents:UIControlEventTouchUpInside];
        self.plusItem.adjustsImageWhenHighlighted = NO; // 去除选择时高亮
        
        [self.plusItem setImage:[UIImage imageNamed:@"呼救"] forState:UIControlStateNormal];
        [self addSubview:self.plusItem];
           
           
      
    }
    return self;
}
- (void)getCurrentDate{
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY/MM"];
    self.date = [formatter stringFromDate:[NSDate date]];
}


- (void)safeAreaInsetsDidChange {
    [super safeAreaInsetsDidChange];
    
    if (!UIEdgeInsetsEqualToEdgeInsets(_oldSafeAreaInsets, self.safeAreaInsets)) {
        [self invalidateIntrinsicContentSize];
        
        if (self.superview) {
            [self.superview setNeedsLayout];
            [self.superview layoutSubviews];
        }
    }
}

- (CGSize)sizeThatFits:(CGSize)size {
    size = [super sizeThatFits:size];
    
    if (@available(iOS 11.0, *)) {
        float bottomInset = self.safeAreaInsets.bottom;
        if (bottomInset > 0 && size.height < 50 && (size.height + bottomInset < 90)) {
            size.height += bottomInset;
        }
    }
    
    return size;
}


- (void)setFrame:(CGRect)frame {
    if (self.superview) {
        if (frame.origin.y + frame.size.height != self.superview.frame.size.height) {
            frame.origin.y = self.superview.frame.size.height - frame.size.height;
        }
    }
    [super setFrame:frame];
}
-(void)layoutSubviews{
    
    [super layoutSubviews];
    CGFloat itemWidth = [UIScreen mainScreen].bounds.size.width / 5;
    //系统自带的按钮类型是UITabBarButton，找出这些类型的按钮，然后重新排布位置，空出中间的位置
    NSInteger index = 0;
    Class class = NSClassFromString(@"UITabBarButton");
    for (UIView *child in self.subviews) {
        if ([child isKindOfClass:class]) {
            CGFloat width = itemWidth;
            CGFloat height = child.frame.size.height;
            CGFloat x = index * width;
            CGFloat y = child.frame.origin.y;
            child.frame = CGRectMake(x, y, width, height);
            index++;
            if (index == 2) {
                CGFloat plusX = itemWidth * index;
                CGFloat plusY = -30;
                self.plusItem.frame = CGRectMake(plusX, plusY, itemWidth, itemWidth);
                index ++;
            }
        }
    }
    [self bringSubviewToFront:self.plusItem];
}

#pragma mark -


#pragma mark - 处理超出区域点击无效的问题
-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (self.hidden) {
        return [super hitTest:point withEvent:event];
    } else {
        //转换坐标
        CGPoint tempPoint = [self convertPoint:point toView:self.plusItem];
        //判断点击的点是否在按钮区域内
        if ([self.plusItem pointInside:tempPoint withEvent:event]) {
            return self.plusItem;
        } else {
            return [super hitTest:point withEvent:event];
        }
    }
}


@end
